package com.ms.service.module.zone.command;

import java.math.BigDecimal;

import org.hibernate.Session;

import com.ms.game.bank.CashChangeWay;
import com.ms.game.bank.db.BankDAO;
import com.ms.game.bank.db.CashLogModel;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.UpdateAttributeList;
import com.ms.player.message.SUpdatePlayerZoneDataByAttributeList;
import com.ms.service.module.account.Account;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.account.message.SAccountCashBonusMessage;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.db.hibernate.manager.HibernateLogManager;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;

public class AccountCommand extends BasicDebugCommand{

	private CommandHelper helper = new CommandHelper(getName());

	static final private int PARAM_VERIFY = 1;
	
	static final private int PARAM_ADD_CASH = 3;

	
	public AccountCommand() {
		helper.addParam(PARAM_VERIFY, "verify", "");
		helper.addParam(PARAM_ADD_CASH, "addcash", "[cash]");
	}
	
	
	@Override
	public String execute() {
		int index = helper.getCommandIndex(commandValue);
		
		BasicZoneModule module = getModule();
		if (module == null) {
			return "can't get current module.";
		}

		Player player = getPlayer();
		if (player == null) {
			return "can't get current player.";
		}
		
		Account account = AccountDAO.loadAccountByAccountId(player.getAccountId());
		if(account == null){
			return "can't get account data.";
		}
		
		switch (index) {
		case PARAM_VERIFY:
			
			if(commandValue.length == 2){
				//自己的
			} else if(commandValue.length == 3){
				//找目標Account
				account = AccountDAO.loadAccountByCooperatorAccountId(commandValue[2]);
				if(account == null){
					return "target account not found";
				}


			}
			
			account.setBindPhone("true");
			GameUtility.writeCashUpdateConsoleLog("AccountCommand.execute()", account.getAccountId().toString(), 0, 0, account.getCash());
			AccountDAO.saveOrUpdateAccount(account);
			
			if(account.getPlayers() != null){
				for (GUID playerId : account.getPlayers()) {
					GUID sessionId = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(playerId);
					if(sessionId == null) {
						continue;
					}
					//送給client
					UpdateAttributeList list = new UpdateAttributeList();
					list.addUpdateValue(PlayerAttributeType.AUTHENTICATE, Boolean.parseBoolean(account.getBindPhone()));
					
					String zoneId = PlayerCommonInfoManager.getInstance().getZoneIdBySession(sessionId);
					if(GameUtility.isHaveString(zoneId)){
						
						SUpdatePlayerZoneDataByAttributeList updateData = new SUpdatePlayerZoneDataByAttributeList();
						updateData.setPlayerSessionId(sessionId);
						updateData.setUpdatedAttrList(list.getUpdateList());
						updateData.setAuthenticate(true);
						//送到所在的Zone上更新資料
						GroupMessage updateMsg = new GroupMessage();
						updateMsg.setMessageContent(updateData);
						updateMsg.setDestModuleID(zoneId);
						updateMsg.setMessageType(MessageTypes.S_ZONE_UPDATE_PLAYER_ATTRIBUTE_DATA);
						MessageAction.sendGroupMessage(updateMsg);
					}
					
					//送給Client
					list.sendToClient(playerId, sessionId, player.getUserType());//下指令的角色類型
				}
			}
			
			
			
			
			
		
			break;
		case PARAM_ADD_CASH:
			int cash = getParamToInt(2);
			if(cash == 0){
				return "cash is 0";
			}
			
			String orderId = GUIDFactory.createUUID(GUIDType.TYPE_CASH_POINT).toString();

			GUID accountID = account.getAccountId();


			CashChangeWay cashChangeWay = cash > 0 ? CashChangeWay.ADD_PAY_MONEY_BACK_BY_GMTOOL : CashChangeWay.SUB_DEDUCT_BY_GMTOOL; 
			
			if (cash < 0) {
				// 扣錢的話 值要是負的
				cash = -cash;
			}

			// cash變化
			long cashBeforeChange = account.getCash();
			long cashChangeValue = cash;
			long cashAfterChange = cashBeforeChange + cashChangeValue;


			// 修改account cash
			GameUtility.writeCashUpdateConsoleLog("AccountCommand.execute()", accountID.toString(), cashBeforeChange, cashChangeValue, cashAfterChange);
			BankDAO.UpdateAccountCashAndBonus(accountID, cashAfterChange, BigDecimal.ZERO);

			// account扣點後的log
			CashLogModel logModel = new CashLogModel();
			logModel.setOrderId(orderId);
			logModel.setAccountId(accountID.toString());
			 
			logModel.setCashBeforeChange(cashBeforeChange);
			logModel.setCashChangeValue(Math.abs(cashChangeValue));
			logModel.setCashAfterChange(cashAfterChange);

			logModel.setChangeWay(cashChangeWay.getId());
			logModel.setChangeWayDesc(cashChangeWay.getDesc());
			
			Session sessionlog = HibernateLogManager.txBegin();
			sessionlog.save(logModel);
			HibernateLogManager.txCommitFailRollbackClearSession();

			SAccountCashBonusMessage accountCashBonusMessage = new SAccountCashBonusMessage();
			accountCashBonusMessage.cashValue = cashAfterChange;
			
			break;

		default:
			break;
		}
		
		
	
		
		return "ok";
	}

	@Override
	public String getName() {
		return "account";
	}

	@Override
	public String getDescription() {
		return null;
	}

	@Override
	public String getShortDescription() {
		return null;
	}
}
